package com.shanghaionstar.user.presenter;

import android.os.Bundle;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.mag.enumtype.CategoryType;
import com.onstar.cn.more.response.GetContentListResponseType;
import com.shanghaionstar.activity.webview.handler.CommonWebViewActivity;
import com.shanghaionstar.activity.webview.handler.DetailContentWebHandler;
import com.shanghaionstar.activity.webview.handler.IWebType;
import com.shanghaionstar.activity.webview.handler.WebDataEntity;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.user.iview.IBuickCareView;
import com.shanghaionstar.user.service.BuickCareService;
import com.shanghaionstar.utils.BusinessUtil;

import rx.functions.Action1;

/**
 * Created by 7828wc on 2016/6/29.
 */
public class BuickCarePresenter extends BasePresenter<GetContentListResponseType> {
    private BaseActivity mContext;
    private CategoryType categoryType;
    private BuickCareService service;
    private IBuickCareView mBuickView;
    private GetContentListResponseType contentHeaderType;

    public BuickCarePresenter(IMVPScheduler mvpScheduler) {
        super(mvpScheduler);
    }

    @Override
    public void setView(IMVPView<GetContentListResponseType> view) {
        super.setView(view);
        super.setView(view);
        mContext = (BaseActivity) view.onActivity();
        mBuickView = (IBuickCareView) view;
        service = new BuickCareService(mContext);
    }


    public void getContentList() {
        mBuickView.setLoading(true);
        subscription = service.getContentList(categoryType).subscribeOn(mvpScheduler.backgroundThread()).observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<GetContentListResponseType>() {
                    @Override
                    public void call(GetContentListResponseType contentListResponse) {
                        mBuickView.setLoading(false);
                        contentHeaderType = contentListResponse;
                        mBuickView.initContentListPage(contentListResponse);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        mBuickView.setLoading(false);
                        if (throwable != null)
                            mBuickView.onResponseError((OnStarException) throwable);
                    }
                });
    }

    public void setCategoryType(CategoryType categoryType) {
        this.categoryType = categoryType;
    }

    public void onTitleClick(FunctionNameFindId functionId, CategoryType categoryType) {
        BusinessUtil.sendReportByClickType(mContext,
                functionId.getStringValue());
        setCategoryType(categoryType);
        getContentList();
    }
//
    public void onItemClick(int position){
        try {
            if (contentHeaderType.getContentHeaderList() != null
                    && contentHeaderType.getContentHeaderList().size() > 0) {
                String id = contentHeaderType.getContentHeaderList().get(position).getContentNUM();
//                BuickCareContentDetailFragment buickCareContentDetailFragment = new BuickCareContentDetailFragment();
                Bundle bundle = new Bundle();
                bundle.putString("detailId", id);
                bundle.putSerializable("categoryType", categoryType);
                WebDataEntity entity = new WebDataEntity();
                entity.setClzName(DetailContentWebHandler.class.getName());
                entity.what = IWebType.TYPE_CONTENT_ID;
                CommonWebViewActivity.launchCommonWeb(mContext,bundle,entity);
//                buickCareContentDetailFragment.setArguments(bundle);
//                mContext.changeFragment(mContext.getSupportFragmentManager(), buickCareContentDetailFragment,
//                        BuickCareContentDetailFragment.class, true);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




    public void initData() {
        categoryType = CategoryType.BUICK_CARE_JPFW;
        getContentList();
    }
}
